package com.cnpinxi.service;

import com.cnpinxi.dao.ConsigneeInfoMapper;
import com.cnpinxi.domain.ConsigneeInfo;
import com.cnpinxi.myRunTime.MyRunTimeExcption;
import com.cnpinxi.service.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by Baggio on 2017-12-8.
 */
@Service
public class ConsigneeInfoServiceImpl implements com.cnpinxi.service.ConsigneeInfoService {
    @Autowired
    ConsigneeInfoMapper consigneeInfoMapper;

    private void insertConsigneeInfo(ConsigneeInfo consigneeInfo) {
        consigneeInfoMapper.insertConsigneeInfo(consigneeInfo);
    }

    private void updateConsigneeInfo(ConsigneeInfo consigneeInfo) {

        ConsigneeInfo consigneeInfo1 = consigneeInfoMapper.selectConsigneeInfoById(consigneeInfo.getTid());

        if (consigneeInfo1 == null) {
            insertConsigneeInfo(consigneeInfo);
            return;
        }
        if (StringUtils.isNotBlank(consigneeInfo.getConsigneePhone())) {
            consigneeInfo1.setConsigneePhone(consigneeInfo.getConsigneePhone());
        }
        if (StringUtils.isNotBlank(consigneeInfo.getConsigneeAddress())) {
            consigneeInfo1.setConsigneeAddress(consigneeInfo.getConsigneeAddress());
        }
        if (StringUtils.isNotBlank(consigneeInfo.getConsigneeName())) {
            consigneeInfo1.setConsigneeName(consigneeInfo.getConsigneeName());
        }

        consigneeInfoMapper.updateConsigneeInfoById(consigneeInfo1);
    }

    @Override
    public void modifyConsigneeInfo(ConsigneeInfo consigneeInfo) throws MyRunTimeExcption {
        if (consigneeInfo == null) {
            throw new MyRunTimeExcption("收货人信息为空！");
        }
        if (consigneeInfo.getTid() == null) {
            insertConsigneeInfo(consigneeInfo);
            return;
        }
        updateConsigneeInfo(consigneeInfo);
    }

    @Override
    public ConsigneeInfo selectConsigneeInfoByOrderId(Long orderId) throws MyRunTimeExcption {
        return consigneeInfoMapper.selectConsigneeInfoByOrderId(orderId);
    }
}
